package com.lesso.zgs.sunflower.executor.handler;

import com.lesso.zgs.sunflower.pojo.vo.EmployeeVO;
import com.lesso.zgs.sunflower.service.IEmployeeService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * SunflowerJob
 *
 * @author zqz
 * @since 2025-07-08
 */
@Component
@Slf4j
public class SunflowerJob {
    @DubboReference
    private IEmployeeService employeeService;

    @XxlJob("sunflower-job")
    public void execute(String s) {
        log.info("定时任务时间，当前时间：{}，请求参数：{}", LocalDateTime.now(), XxlJobHelper.getJobParam());
        XxlJobHelper.log("传入参数：{} {}", s, XxlJobHelper.getJobParam());
        XxlJobHelper.log("hello xxl job");
        log.info("hello");
    }

    /**
     * 定时分页拉取所有员工并打印员工数量和部分信息
     */
    @XxlJob("printAllEmployees")
    public void printAllEmployees() {
        int pageNum = 1;
        int pageSize = 20;
        int total = 0;
        while (true) {
            int finalPageNum = pageNum;
            com.baomidou.mybatisplus.plugins.Page<com.lesso.zgs.sunflower.pojo.vo.EmployeeVO> page =
                    employeeService.page(new com.lesso.zgs.sunflower.pojo.dto.EmployeeQueryDTO() {{
                        setCurrent(finalPageNum);
                        setSize(pageSize);
                    }});
            if (page == null || page.getRecords() == null || page.getRecords().isEmpty()) {
                break;
            }
            total += page.getRecords().size();
            // 打印每页前3个员工信息
            page.getRecords().stream().limit(3).forEach(e ->
                    log.info("员工ID:{} 姓名:{} 工号:{}", e.getId(), e.getName(), e.getEmployeeNo())
            );
            if (!page.hasNext()) {
                break;
            }
            pageNum++;
        }
        log.info("本次定时任务共查询到员工数量: {}", total);
        XxlJobHelper.log("本次定时任务共查询到员工数量: {}", total);
    }
}
